#include "PreCompile.hpp"

static void init()
{
  BuildNG::init_shared();
  BuildNG::init_libbuildng("Content/Win32Settings.xml");
  BuildNG::init_clang();
}

static void shutdown()
{
  BuildNG::shutdown_clang();
  BuildNG::shutdown_libbuildng();
  BuildNG::shutdown_shared();
}

int main(int argc,char *argv[])
{
  try
  {
    init();

    // parse the arguments
    bool project_specified = false;
    Glib::ustring project_file_name;
    for(int i = 1;i < argc;i++)
    {
      if(argv[i][0] == '-')
      {
        // it's a flag
        assert(!"not implemented yet");
      }
      else
      {
        // it's the project file name.
        if(project_specified)
          throw BuildNG::Exception("Only one project file should be specified.");

        project_specified = true;
        project_file_name = argv[i];
      }
    }

    if(!project_specified)
      throw BuildNG::Exception("No project specified.");

    Glib::RefPtr<BuildNG::Project> project = BuildNG::Project::load_from_file(project_file_name);
    project->build(Glib::build_filename(project->get_root_dir(),"output"),BuildNG::StdErrorListener::create());
    
    shutdown();
    
    return 0;
  }
  catch(const Glib::Exception &err)
  {
    shutdown();
    
    std::cerr << err.what() << std::endl;
    return 1;
  }
  catch(const std::exception &err)
  {
    shutdown();
    
    std::cerr << err.what() << std::endl;
    return 1;
  }
}
